package codechicken.wirelessredstone.core;

import net.minecraft.src.*;

public class CorePacketHandler implements IWirelessPacketHandler
{
	public void handlePacket(World world, EntityPlayer player, PacketRedstoneEther packet)
	{
		switch(packet.getType())
		{
			case 4:
				int freq = packet.readShort();
				String name = packet.readString();
				int colourid = packet.readByte();
				RedstoneEther.setFreqName(freq, name);
				RedstoneEther.setFreqColour(freq, colourid);
				sendSetFreqInfoTo(null, freq, name, colourid);
			break;
			case 5:
				int slot = packet.readShort();
				int itemID = packet.readShort();
				int quantity = packet.readByte();
				int damage = packet.readShort();
				
				if(quantity == 0)
				{
					player.inventory.mainInventory[slot] = null;
				}
				else
				{
					player.inventory.mainInventory[slot] = new ItemStack(itemID, quantity, damage);
				}
			break;
		}
	}
	
	public static void sendSetFrequencyRangeTo(EntityPlayer player, int startfreq, int endfreq, boolean jam)
	{
		PacketRedstoneEther packet = new PacketRedstoneEther(3);
		packet.writeShort((short) startfreq);
		packet.writeShort((short) endfreq);
		packet.writeBoolean(jam);

		CoreProxy.sendPacketTo(player, packet);
	}

	public static void sendPublicFrequencyTo(EntityPlayer player, int publicfrequencyend)
	{
		PacketRedstoneEther packet = new PacketRedstoneEther(2);
		packet.writeShort((short) publicfrequencyend);

		CoreProxy.sendPacketTo(player, packet);
	}

	public static void sendJamAllFrequenciesTo(EntityPlayer player)
	{
		PacketRedstoneEther packet = new PacketRedstoneEther(1);
		
		CoreProxy.sendPacketTo(player, packet);
	}

	public static void sendSetFreqInfoTo(EntityPlayer player, int freq, String name, int colourid)
	{
		PacketRedstoneEther packet = new PacketRedstoneEther(4);
		packet.writeShort((short) freq);
		packet.writeString(name);
		packet.writeByte((byte)colourid);
		CoreProxy.sendPacketTo(player, packet);
	}
	
	public static void sendResetClientTo(EntityPlayer player)
	{
		PacketRedstoneEther packet = new PacketRedstoneEther(6);
		
		CoreProxy.sendPacketTo(player, packet);
	}

	public static void sendJamPlayerPacketTo(EntityPlayer player, boolean jam)
	{
		PacketRedstoneEther packet = new PacketRedstoneEther(7);
		packet.writeBoolean(jam);
		
		CoreProxy.sendPacketTo(player, packet);
	}

	public static void sendSetSlot(int slot, int itemID, int i, int freq)
	{
		
	}
}
